// pages/index/schooldetail/schooldetail.js

import {
    GetSchoolDetail, // 查院校详情
    GetCollect, // 收藏
    GetCancelCollect, // 取消收藏
    GetContrastAdd, // 对比
    GetContrastDel, // 取消对比
    GetContrastList, // 对比列表
    GetZsjhlist, // 招生计划
    GetXklist, // 选科
    GetPclist, // 批次
    GetStudentgrades, // 我的成绩查询
    GetHislist, // 历史成绩
} from '../../../api/index'
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemArray: ['综合类', '强基', '985', '211', '双一流'],
        ndicatorDots: false,
        autoplay: true,
        interval: 4000,
        duration: 800,
        // bannerArray:['https://img1.baidu.com/it/u=3245612049,2924146567&fm=253&fmt=auto&app=138&f=PNG?w=1301&h=500',
        //             'https://img1.baidu.com/it/u=3245612049,2924146567&fm=253&fmt=auto&app=138&f=PNG?w=1301&h=500',
        //             'https://img1.baidu.com/it/u=3245612049,2924146567&fm=253&fmt=auto&app=138&f=PNG?w=1301&h=500',
        //             'https://img1.baidu.com/it/u=3245612049,2924146567&fm=253&fmt=auto&app=138&f=PNG?w=1301&h=500'],
        dataSourceArray: [{
                dictValue: "0",
                dictLabel: "院校简介"
            },
            {
                dictValue: "1",
                dictLabel: "招生计划"
            },
            {
                dictValue: "2",
                dictLabel: "招生简章"
            },
            {
                dictValue: "3",
                dictLabel: "历史分数"
            }
        ],
        tabSelectIndex: 0,
        model: {},
        introList: [{
                title: "创建时间",
                detial: ''
            },
            {
                title: "隶属于",
                detial: ''
            },
            {
                title: "学科层次",
                detial: ''
            },
            {
                title: "博士点",
                detial: ''
            },
            {
                title: "硕士点",
                detial: ''
            },
            {
                title: "重点学科",
                detial: ''
            },
            {
                title: "学生人数",
                detial: ''
            },
            {
                title: "院士人数",
                detial: ''
            }
        ],
        show: false,
        compareShow: false,
        selectIndex: 0, // 0 院校简介  1优势专业  2历史沿革 3知名校友 4师资力量
        schoolId: '',
        compareArray: [],
        compareAddArray: [], // 加入对比个数
        planArray:[], // 招生计划
        

        screeSelectIndex:0,

        lot:'物理类',
        makeLot:'物理类',
        lot_show:false,

        // 批次
        piciArray:[],
        pici:'',
        makePici:'',
        picValue:'',
        makePiciValue:'',
        
        // 批次
        historyLot:'物理类',
        makeHistoryLot:'物理类',
        historyLot_show:false,
    pici_show:false,

        // 选科
        xuankeArray:[],
        xuanke:'',
        makeXuanke:'',

        // 历史分数
        historyList:[],

        yearshow:false,
        year:'2023',
        yearArray:[{
            name: '2023年',
        },{
            name: '2022年',
        },{
            name: '2021年',
        }],
        openVip:0,
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            openVip:app.globalData.openVip
        })
        console.log('openvip=' +  this.data.openVip);
    },

    // 开通vip
    openVipAction() {
        wx.navigateTo({
          url: '/pages/mine/openvip/openvip',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
        console.log('safeAreaBottom')
        console.log(this.data.safeAreaBottom)
        this.setData({
            schoolId: options.id
        })
        let openid = wx.getStorageSync('openId') || '';
        this._GetSchoolDetail({
            id: options.id,
            openId:openid
        }, true)
        
        if (openid != '') {
            this._GetContrastList(openid, false)
        }
        this._GetStudentgrades('?openId=' + openid, true)
        
        
        
        this._GetZsjhlist("?schoolId=" + this.data.schoolId + "&pcdm=" + this.data.makePiciValue +"&zyzksyqjc=" + (this.data.makeXuanke == '全部' ? '' : this.data.makeXuanke) + '&klmc=' + this.data.makeLot, false);
        this._GetHislist("?schoolId=" + this.data.schoolId + '&pcmc=' + this.data.makeHistoryLot + "&zsYear=" + this.data.year, false);
    },

    onChange(event) {
        console.log(event)
        this.setData({
            tabSelectIndex: event.detail.name
        })
    },

    yearAction(){
        this.setData({
            yearshow:true
        })
    },
    onClose() {
        this.setData({ yearshow:false });
      },
    onSelect(event) {
        console.log(event.detail);
        this.onClose()
        if (event.detail.name == '2023年') {
            this.setData({
                year:2023
            })
        } else if (event.detail.name == '2022年') {
            this.setData({
                year:2022
            })
        } else if (event.detail.name == '2021年') {
            this.setData({
                year:2021
            })
        }

        this._GetHislist("?schoolId=" + this.data.schoolId + '&pcmc=' + this.data.makeHistoryLot + "&zsYear=" + this.data.year, false);

      },

    // 招生计划，物理历史筛选点击事件
    screenPhysicsAction() {
        let that = this;
        this.setData({
            screeSelectIndex:0,
            lot:that.data.makeLot,
            lot_show:true
        })
    },

    // 招生计物理历史划选中事件
    lotActioon(event) {
        console.log(event)
        this.setData({
            lot:event.currentTarget.dataset.item
        })
    },

    // 招生计划确定
    makeLotAction() {
        console.log(this.data.lot)
        let that = this;
        if (this.data.screeSelectIndex == 0) {
            this.setData({
                makeLot:that.data.lot,
                lot_show:false
            })
            this._GetPclist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
            this._GetXklist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
        } else if (this.data.screeSelectIndex == 1) {
            this.setData({
                makePici:that.data.pici,
                makePiciValue:that.data.picValue,
                lot_show:false
            })
        } else {
            this.setData({
                makeXuanke:that.data.xuanke,
                lot_show:false
            })
        }
        console.log("?schoolId=" + this.data.schoolId + "&pcdm=" + this.data.makePiciValue +"&zyzksyqjc=" + (this.data.makeXuanke == '全部' ? '' : this.data.makeXuanke)+ '&klmc=' + this.data.makeLot)
        this._GetZsjhlist("?schoolId=" + this.data.schoolId + "&pcdm=" + this.data.makePiciValue +"&zyzksyqjc=" + (this.data.makeXuanke == '全部' ? '' : this.data.makeXuanke) + '&klmc=' + this.data.makeLot, false);
    },


    // 历史分数，批次筛选点击事件
    historyScreenLotAction() {
        let that = this;
        this.setData({
            historyLot_show:true,
            historyLot:that.data.makeHistoryLot,
        })
    },

    historylotActioon(event) {
        console.log(event)
        this.setData({
            historyLot:event.currentTarget.dataset.item
        })
    },

    // 确定
    makeHistoryLotAction() {
        let that = this;
        this.setData({
            makeHistoryLot:that.data.historyLot,
            historyLot_show:false
        })
        this._GetHislist("?schoolId=" + this.data.schoolId + '&pcmc=' + this.data.makeHistoryLot + "&zsYear=" + this.data.year, false);
    },

     // 招生计划，批次筛选点击事件
     screenLotAction() {
        let that = this;
        this.setData({
            screeSelectIndex:1,
            lot_show:true,
            pici:that.data.makePici,
            picValue:that.data.makePiciValue,
        })
    },

    // 招生计划，批次、选科筛选点击事件
    showSelectAction(event) {
        console.log(event)
        let item = event.currentTarget.dataset.item
        if (this.data.screeSelectIndex == 1) {
            this.setData({
                pici:item.pcmc,
                picValue:item.pcdm
            })
        } else {
            this.setData({
                xuanke:item.zyzksyqjc,
            })
        }
    },

     // 招生计划，选科筛选点击事件
     screenSubjectAction() {
        let that = this;
        this.setData({
            screeSelectIndex:2,
            lot_show:true,
            xuanke:that.data.makeXuanke,
        })
    },

    // 收藏
    collectAction() {
        let openid = wx.getStorageSync('openId') || '';
        if (openid == '') {
            wx.navigateTo({
                url: '/pages/login/login',
            })
            return
        }
        if (this.data.model.isCollect == 0) {
            this.setData({
                'model.isCollect': 1
            })
            this._GetCollect({
                openId: openid,
                collectType: '1',
                schoolId: this.data.schoolId
            }, false)
        } else {
            this.setData({
                'model.isCollect': 0
            })
            this._GetCancelCollect({
                openId: openid,
                collectType: '1',
                schoolId: this.data.schoolId
            }, false)
        }

    },

    // 对比
    compareAction() {

        let openid = wx.getStorageSync('openId') || '';
        if (openid == '') {
            wx.navigateTo({
                url: '/pages/login/login',
            })
            return
        }
        if (this.data.model.isContrast == 0) {
            this.setData({
                'model.isContrast': 1
            })
            this._GetContrastAdd({
                openId: openid,
                schoolId: this.data.schoolId
            }, true)
        } else {
            this.setData({
                'model.isContrast': 0
            })
            this._GetContrastDel({
                openId: openid,
                schoolId: this.data.schoolId,
                reserve1: 'one'
            }, false)
        }
    },

    _GetZsjhlist(data, isLoading) {
        console.log('招生计划')
        GetZsjhlist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                this.setData({
                    planArray: array
                });

            } else {
                
            }
        })
    },

    // 院校对比展示弹窗
    compareShowAction() {
        if (this.data.compareArray.length ==0) {
            Toast('请先加入对比院校')
            return
        }
        this.setData({
            compareShow: true
        })
    },


    //预览图片，放大预览
    preview(event) {
        console.log(event.currentTarget.dataset.index)
        let index = event.currentTarget.dataset.index
        console.log(this.data.model.mienList)
        let bannerArray = []
        this.data.model.mienList.forEach(item => {
            bannerArray.push(item.imgUrl)
        })
        wx.previewImage({
            current: bannerArray[index], // 当前显示图片的http链接
            urls: bannerArray // 需要预览的图片http链接列表
        })
    },

    // 关闭院校详情
    close() {
        this.setData({
            show: false,
            compareShow: false,
            lot_show:false,
            historyLot_show:false
        })
    },

    swiperChange(e) {
        let current = e.detail.current;
        // console.log(current, '轮播图')
        let that = this;
        that.setData({
            swiperCurrent: current,
        })
    },

    //  选科
    _GetXklist(data, isLoading) {
        GetXklist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.data
                if (array.length !=0) {
                    let item =  array[0]    
                    array.unshift({zyzksyqjc:'全部'});
                    this.setData({
                        xuankeArray: array,
                        xuanke: '全部',
                        makeXuanke:'全部',
                    })
                }
            } else {
                // Toast(res.msg)
            }
        })
    },

    //  批次
    _GetPclist(data, isLoading) {
        GetPclist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let mutableArrasy = [{pcmc:"全部",pcdm:''}]
                let array = res.data
                if (array.length != 0) {
                    this.setData({
                        piciArray: mutableArrasy.concat(array),
                        pici: '批次',
                        makePici:'批次'
                    })
                }
            } else {
                Toast(res.msg)
            }
        })
    },

    //  历史分数
    _GetHislist(data, isLoading) {
        GetHislist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                this.setData({
                    historyList:res.rows
                })
            } else {
                Toast(res.msg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    // 院校详情点击事件
    customIntroActionChildren() {
        this.setData({
            show: true,
            selectIndex: 0
        })
    },

    // 优势专业点击事件
    professionActionChildren() {
        this.setData({
            show: true,
            selectIndex: 1
        })
    },

    // 历史沿革
    historyActionChildren() {
        this.setData({
            show: true,
            selectIndex: 2
        })
    },

    // 知名校友
    alumnusActionChildren() {
        this.setData({
            show: true,
            selectIndex: 3
        })
    },

    // 师资力量
    teacherActionChildren() {
        this.setData({
            show: true,
            selectIndex: 4
        })
    },

    // 对比院校列表点击事件
    compareSchoolChooseAction(event) {
        let index = event.currentTarget.dataset.index;
        let choose = this.data.compareArray[index].choose;
        let schoolIdRemove = this.data.compareArray[index].schoolId;
        let filteredArray;
        let array = this.data.compareAddArray
        if (choose == 1) {
            // 过滤数组，移除指定元素
            filteredArray = array.filter(item => item !== schoolIdRemove);
        } else {
            array.push(schoolIdRemove)
            filteredArray = array
        }
        this.setData({
            compareAddArray: filteredArray
        })
        console.log(this.data.compareAddArray)
        // 切换 choose 的值
        choose = choose === 1 ? 0 : 1;
        // 更新数据
        let newData = this.data.compareArray.slice(); // 克隆数组
        newData[index] = {
            ...newData[index],
            choose
        }; // 更新 choose 属性
        this.setData({
            compareArray: newData
        });
    },

    // 移除对比院校
    removeCompareSchoolAction(event) {
        let index = event.currentTarget.dataset.index;
        let schoolId = this.data.compareArray[index].schoolId;
        let openid = wx.getStorageSync('openId') || '';
        console.log(this.data.compareAddArray)
        console.log(schoolId)
        if (schoolId == this.data.schoolId) {
            this.setData({
                'model.isContrast': 0
            })
        }
        // 获取当前数组
        let compareArray = this.data.compareArray;
        // 移除指定下标的元素
        compareArray.splice(index, 1);
        // 更新数据
        this.setData({
            compareAddArray: this.data.compareAddArray.filter(item => item !== schoolId),
            compareArray: compareArray
        });
        this._GetContrastDel({
            openId: openid,
            schoolId: schoolId,
            reserve1: 'one'
        }, false)
    },
    // 清空对比
    compareClearAction() {
        // if (this.data.compareAddArray.length == 0) {
        //     return
        // }
        console.log('清空')
        let that = this
        Dialog.confirm({
                title: '清空院校',
                message: '是否确认清空',
            })
            .then(() => {
                // on confirm
                that.setData({
                    'model.isContrast': 0
                })
                let openid = wx.getStorageSync('openId') || '';
                that._GetContrastDel({
                    openId: openid,
                    schoolId: '',
                    reserve1: 'all'
                }, false)
            })
            .catch(() => {
                // on cancel
            });

    },

    // 开始对比
    compareStartAction() {
        if (this.data.compareAddArray.length <= 1) {
            return
        }
        this.setData({
            compareShow: false
        })
        let reserve = 'one'
        let reserve2 = ''
        if (this.data.compareArray.length == this.data.compareAddArray.length) {
            reserve = 'all'
            reserve2 = ''
        } else {
            reserve = 'one'
            reserve2 = this.data.compareAddArray.join(','); // 使用逗号作为分隔符
        }
        wx.navigateTo({
            url: '/pages/index/schoolCompare/schoolCompare?reserve1=' + reserve + "&reserve2=" + reserve2,
        })
    },


    // 院校详情
    _GetSchoolDetail: function (data, isLoading) {
        GetSchoolDetail(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                this.setData({
                    model: res.data,
                    ['introList[' + 0 + '].detail']: res.data.createYear,
                    ['introList[' + 1 + '].detail']: res.data.heAttach,
                    ['introList[' + 2 + '].detail']: res.data.heBatch,
                    ['introList[' + 3 + '].detail']: res.data.numDoctor,
                    ['introList[' + 4 + '].detail']: res.data.numMaster,
                    ['introList[' + 5 + '].detail']: res.data.numImpPro,
                    ['introList[' + 6 + '].detail']: res.data.numStu,
                    ['introList[' + 7 + '].detail']: res.data.numAcade,
                })
            } else {
                Toast(res.msg)
            }
        })
    },

    // 收藏
    _GetCollect: function (data, isLoading) {
        GetCollect(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                // Toast("收藏成功")
            } else {
                // Toast(res.msg)
            }
        })
    },

    // 收藏
    _GetCancelCollect: function (data, isLoading) {
        GetCancelCollect(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                Toast("取消收藏成功")
            } else {
                Toast(res.msg)
            }
        })
    },

    // 对比
    _GetContrastAdd: function (data, isLoading) {
        GetContrastAdd(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                Toast("加入对比成功")
                let openid = wx.getStorageSync('openId') || '';
                this._GetContrastList(openid, false)
            } else {
                Toast(res.msg)
            }
        })
    },

    // 取消对比
    _GetContrastDel: function (data, isLoading) {
        GetContrastDel(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                Toast("取消对比成功")
                let openid = wx.getStorageSync('openId') || '';
                this._GetContrastList(openid, false)
            } else {
                Toast(res.msg)
            }
        })
    },

    // 对比列表
    _GetContrastList: function (data, isLoading) {
        GetContrastList(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            if (code == 200) {
                this.setData({
                    compareAddArray:[],
                    compareArray: res.rows
                })
            } else {
                Toast(res.msg)
            }
        })
    },

    // 我的成绩查询
    _GetStudentgrades(data, isLoading) {
        GetStudentgrades(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                if (array.length != 0) {
                    let model = array[0]
                    if (model.isWuli == 1) {
                        this.setData({
                            lot: '物理类',
                            makeLot:'物理类',
                        })
                    } else if (model.isWuli == 0){
                        this.setData({
                            lot: '历史类',
                            makeLot:'历史类'
                        })
                    } else {
                        this.setData({
                            lot: '物理类',
                            makeLot:'物理类',
                        })
                    }
                    this._GetPclist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
                    this._GetXklist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
                } else {
                    this._GetPclist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
                    this._GetXklist("?schoolId=" + this.data.schoolId + "&klmc=" + this.data.makeLot, false);
                }
                
            } else {
                // Toast(res.msg)
            }
        })
    },



})